﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;

class SplitAndMerge
{
    public const string FILENAME = "Test.mp3";
    public const int ONE_MB = 1024 * 1024;

    public static void SplitFile(string fileName)
    {
        using (FileStream readStream = new FileStream(fileName, FileMode.Open, FileAccess.Read))
        {
            FileInfo file = new FileInfo(fileName);
            long fileSize = file.Length;
            long firstPartSize = fileSize / 2;
            long secondPartSize = fileSize - firstPartSize;
            string newFilePart1 = string.Format("{0}-part_1.{1}", fileName.Split('.')[0], fileName.Split('.')[1]);
            string newFilePart2 = string.Format("{0}-part_2.{1}", fileName.Split('.')[0], fileName.Split('.')[1]);
            //making the first file
            using (FileStream writeStream = new FileStream(newFilePart1, FileMode.OpenOrCreate, FileAccess.Write))
            {
                while (firstPartSize != 0)
                {
                    int bufferSize;
                    if (firstPartSize < ONE_MB)
                    {
                        bufferSize = (int)firstPartSize;
                    }
                    else
                    {
                        bufferSize = ONE_MB;
                    }
                    byte[] buffer = new byte[bufferSize];
                    readStream.Read(buffer, 0, bufferSize);
                    writeStream.Write(buffer, 0, bufferSize);
                    firstPartSize -= bufferSize;
                }
            }
            //making the 2nd file
            using (FileStream writeStream = new FileStream(newFilePart2, FileMode.OpenOrCreate, FileAccess.Write))
            {
                while (secondPartSize != 0)
                {
                    int bufferSize;
                    if (secondPartSize < ONE_MB)
                    {
                        bufferSize = (int)secondPartSize;
                    }
                    else
                    {
                        bufferSize = ONE_MB;
                    }
                    byte[] buffer = new byte[bufferSize];
                    readStream.Read(buffer, 0, bufferSize);
                    writeStream.Write(buffer, 0, bufferSize);

                    secondPartSize -= bufferSize;
                }
            }
        }
    }

    public static void MergeFilesToOriginal(string fileName)
    {
        string FilePart1 = string.Format("{0}-part_1.{1}", fileName.Split('.')[0], fileName.Split('.')[1]);
        string FilePart2 = string.Format("{0}-part_2.{1}", fileName.Split('.')[0], fileName.Split('.')[1]);
        string[] nameParts = fileName.Split('.');
        string outputFilename = string.Format("{0}-original.{1}", nameParts[0], nameParts[1]);
        using (FileStream writeStream = new FileStream(outputFilename, FileMode.OpenOrCreate, FileAccess.Write))
        {
            //reading part1 file
            using (FileStream readStream = new FileStream(FilePart1, FileMode.Open, FileAccess.Read))
            {
                byte[] buffer = new byte[ONE_MB];
                byte[] bufferRead;
                while (true)
                {
                    int bytesRead = readStream.Read(buffer, 0, ONE_MB);
                    if (bytesRead == 0)
                    {
                        break;
                    }
                    else if (bytesRead != ONE_MB)
                    {
                        bufferRead = new byte[bytesRead];
                        Array.Copy(buffer, bufferRead, bytesRead);
                    }
                    else
                    {
                        bufferRead = buffer;
                    }
                    writeStream.Write(bufferRead, 0, bytesRead);
                }
            }
            //reading part2 file
            using (FileStream readStream = new FileStream(FilePart2, FileMode.Open, FileAccess.Read))
            {
                byte[] buffer = new byte[ONE_MB];
                byte[] bufferRead;
                while (true)
                {
                    int bytesRead = readStream.Read(buffer, 0, ONE_MB);
                    if (bytesRead == 0)
                    {
                        break;
                    }
                    else if (bytesRead != ONE_MB)
                    {
                        bufferRead = new byte[bytesRead];
                        Array.Copy(buffer, bufferRead, bytesRead);
                    }
                    else
                    {
                        bufferRead = buffer;
                    }
                    writeStream.Write(bufferRead, 0, bytesRead);
                }
            }
        }
    }

    static void Main()
    {
        SplitFile(FILENAME);
        MergeFilesToOriginal(FILENAME);
    }
}

